<template>
	<div>
		<Card class="content-card">
			<Tabs :animated="false">
				<TabPane label="配件报价信息" name="info">

					<Form ref="formValidate" :model="info" :rules="ruleValidate" :label-width="150" class="form_list">

						<FormItem label='状态：'>
							<span>{{stateList[info.returnType]}}</span>
						</FormItem>
						<FormItem label='退货原因：'>
							<span>{{info.refundReason}}</span>
						</FormItem>
						<FormItem label='退款金额：'>
							<span v-if="info.backMoney">￥{{info.backMoney.toFixed(2)}}元</span>
						</FormItem>
						<FormItem label='退款编号：'>
							<span>{{info.orderNum}}</span>
						</FormItem>
						<FormItem label='申请时间：'>
							<span>{{info.createTime}}</span>
						</FormItem>
						<FormItem label='日租费用：' v-if="info.returnType == 0">
							<Input v-model="dayCost" style="width:200px;"></Input>
						</FormItem>

						<template v-if="info.returnType >= 3">
							<FormItem label='发货日期：'>
								<span v-if="info.deliveryDate">{{info.deliveryDate.substr(0,10)}}</span>
							</FormItem>
							<FormItem label='物流公司：'>
								<span>{{info.logisticsCompany}}</span>
							</FormItem>
							<FormItem label='物流单号：'>
								<span>{{info.singleNumber}}</span>
							</FormItem>
							<FormItem label='发货人：'>
								<span>{{info.consignor}}</span>
							</FormItem>
							<FormItem label='手机：'>
								<span>{{info.consignorTel}}</span>
							</FormItem>
							<FormItem label='备注：'>
								<span>{{info.note}}</span>
							</FormItem>
						</template>

						<FormItem label='日租费用：' v-if="info.returnType >= 1">
							<span v-if="info.dayCost">￥{{info.dayCost.toFixed(2)}}元</span>
						</FormItem>
						<template v-if="info.returnType >= 3">
							<FormItem label='租赁天数：'>
								<span v-if="info.leaseDay">{{info.leaseDay}}天</span>
								<span v-else>0</span>
							</FormItem>
							<FormItem label='租赁费用：'>
								<span v-if="info.leaseMoney">￥{{info.leaseMoney.toFixed(2)}}元</span>
								<span v-else>￥0</span>
							</FormItem>
						</template>

						<FormItem label='收货日期：'v-if="info.returnType >= 4">
							<span v-if="info.confirmTime">{{info.confirmTime.substr(0,10)}}</span>
						</FormItem>
						<template v-if="info.returnType >= 5">
							<FormItem label='付款日期：'>
								<span v-if="info.refundDate">{{info.refundDate.substr(0,10)}}</span>
							</FormItem>
							<FormItem label='付款凭证：'>
								<div v-for="item in buyersProof" style="margin-left: 5px;">
									<img :src="item" style="width: 50px; height: 50px;" />
								</div>
							</FormItem>
						</template>

						<div class="button_list">
							<Button type="primary" @click="submitForm" v-if="info.returnType == 0">同意</Button>
							<Button type="primary" @click="confirmForm" v-if="info.returnType == 3">确认收货</Button>
							<Button type="primary" @click="confirmMoney" v-if="info.returnType == 5">确认收款</Button>
							<Button type="primary" @click="cancelForm" style="margin-left: 15px;">返回</Button>
						</div>
					</Form>

				</TabPane>
			</Tabs>
		</Card>
	</div>


</template>

<script>
	import {
		formatDate
	} from '../utils/time.js'
	export default {
		components: {
		},
		data() {
			return {
				partsOutputList: [],
				info: {},
                stateList: {
                    0: "待审核",
                    1: "审核通过",
                    2: "审核未通过",
                    3: "待收货",
                    4: "待退款",
                    5: "待确认",
                    6: "退款完成"
                },
                returnId: '',
                dayCost: '',
                buyersProof: []
			}
		},
        mounted() {
            // 查询配件报价信息
            this.getInfo();
        },
		methods: {
			getInfo() {
                this.returnId = this.$route.query.id;

                this.$http.post('/parts/return/queryid', {id: this.returnId}).then(res => {
                    if (res.status == 200) {
                        if (res.data.code == 200) {
                            this.info = res.data.data;
                            if (res.data.data.buyersProof) {
                                this.buyersProof = res.data.data.buyersProof.split(',')
                            }
                            this.info.createTime = formatDate(new Date(res.data.data.createTime), 'yyyy-MM-dd hh:mm:ss');
                            this.partsOutputList = res.data.data.partsOutputList;
                        }

                    }

                })
			},
            submitForm(type) {
                let data = {
                    returnId: this.returnId,
                    dayCost: this.dayCost,
                    returnType: 1
				}
                this.$http.post('/parts/return/save', data).then(res => {
                    if (res.status == 200) {
                        if (res.data.code == 200) {
                            this.$Message.success('审核成功')
                            setTimeout(() => {
                                this.$router.push('/partsReturnList');
                            }, 500)
                        }
                    }
                    else {
                        this.$Message.error(res.msg);
                    }
                })
            },
            confirmForm() {
                this.$http.post('/parts/return/confirm', {returnId: this.returnId}).then(res => {
                    if (res.status == 200) {
                        if (res.data.code == 200) {
                            this.$Message.success('收货成功')
                            setTimeout(() => {
                                this.$router.push('/partsReturnList');
                            }, 500)
                        }
                    }
                    else {
                        this.$Message.error(res.msg);
                    }
                })
			},
            confirmMoney() {
                let postData = {
                    returnId: this.returnId,
                    sellerReceipt: 1,
                }
                if (this.info.buyersReceipt == 1) {
                    postData.returnType = 6
                }
                setTimeout(() => {
                    this.$http.post('/parts/return/save', postData).then(res => {
                        if (res.data.code == 200) {
                            if (this.info.buyersReceipt == 1) {
                                this.$Message.success("收款成功")
                            } else {
                                this.$Message.success("等待对方确认收款")
                            }
                            setTimeout(() => {
                                location.reload();
                            }, 1000)
                        }
                    })
                }, 1000)
			},
            cancelForm() {
                this.$router.push('/partsReturnList');
            }
		}
	}
</script>

<style scoped>
	.priceTable {
		width: 100%;
		color: #515a6e;
		font-size: 12px;
		border-spacing: 0px;
		border-top: 1px solid #e8eaec;
		border-left: 1px solid #e8eaec;
	}
	.priceTable th {
		height: 40px;
		white-space: nowrap;
		overflow: hidden;
		background-color: #f8f8f9;
		text-overflow: ellipsis;
		vertical-align: middle;
		border-bottom: 1px solid #e8eaec;
		border-right: 1px solid #e8eaec;
	}
	.priceTable td {
		height: 34px;
		white-space: nowrap;
		overflow: hidden;
		padding: 0 10px;
		border-right: 1px solid #e8eaec;
		border-bottom: 1px solid #e8eaec;
		text-align: center;
	}
	.button_list {
		text-align: center;
		margin-top: 15px;
	}
</style>
<style>
	.form_list .ivu-form-item {
		margin-bottom: 5px;
	}
</style>